package com.carleasoft.mps.cms.business.clerk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.cms.business.clerk.bean.DealRecordEntity;
import com.carleasoft.mps.cms.business.clerk.bean.po.*;
import com.carleasoft.mps.cms.business.clerk.bean.vo.*;
import com.carleasoft.mps.cms.business.clerk.service.DealProcessService;
import com.carleasoft.mps.cms.annotation.OpenApi;
import com.carleasoft.mps.cms.business.train.bean.entity.TrainConsume;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.apache.catalina.webresources.WarResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 办事流程
 *
 * @author: yangHao
 * @createDate:2023/5/17
 * @description:
 */
@RestController
@RequestMapping("/deal")
public class DealProcessController {

    @Autowired
    private DealProcessService dealProcessService;


    /**
     * 办事流程分页
     *
     * @param pageQueryPo
     * @return
     */
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody PageQueryPo pageQueryPo) {
        Page<PageQueryVo> page = dealProcessService.pageQuery(new Page(pageQueryPo.getCurrent(), pageQueryPo.getSize()), pageQueryPo);
        return R.ok(page);
    }

    /**
     * 网上办事-门户分页
     *
     * @param pageQueryPo
     * @return
     */
    @PostMapping("/doorPageQuery")
    public R doorPageQuery(@RequestBody PageQueryPo pageQueryPo) {
        Page<PageQueryVo> page = dealProcessService.doorPageQuery(new Page(pageQueryPo.getCurrent(), pageQueryPo.getSize()), pageQueryPo);
        return R.ok(page);
    }


    /**
     * 新增办事流程
     *
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody DealProcessPo dealProcessPo, HttpServletRequest request) {
        boolean isOk = dealProcessService.saveDeal(dealProcessPo, request);
        return R.ok(isOk);
    }

    /**
     * 编辑办事流程
     *
     * @param dealProcessPo
     * @param request
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody DealProcessPo dealProcessPo, HttpServletRequest request) {
        IBeanUtil.paramVerify(dealProcessPo, "processCode");
        boolean isOk = dealProcessService.updateDeal(dealProcessPo, request);
        return R.ok(isOk);
    }

    /**
     * 办事流程 详情
     *
     * @param dealProcessPo
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody DealProcessPo dealProcessPo) {
        IBeanUtil.paramVerify(dealProcessPo, "processCode");
        DealProcessPo dealProcessPos = dealProcessService.detail(dealProcessPo);
        return R.ok(dealProcessPos);
    }

    /**
     * 办事流程 门户详情
     *
     * @param dealProcessPo
     * @return
     */
    @PostMapping("/doorDetail")
    public R doorDetail(@RequestBody DealProcessPo dealProcessPo) {
        IBeanUtil.paramVerify(dealProcessPo, "processCode");
        DealProcessPo dealProcessPos = dealProcessService.detail(dealProcessPo);
        return R.ok(dealProcessPos);
    }

    /**
     * 办事流程 停用/启用
     *
     * @param stopAndStartPo
     * @return
     */
    @PostMapping("/stopAndStart")
    public R stopAndStart(@RequestBody StopAndStartPo stopAndStartPo) {
        IBeanUtil.paramVerify(stopAndStartPo, "processCode,type");
        boolean isOk = dealProcessService.stopAndStart(stopAndStartPo);
        return R.ok(isOk);
    }

    /**
     * 办事流程 删除(逻辑删除)
     *
     * @param dealProcessPo
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody DealProcessPo dealProcessPo) {
        IBeanUtil.paramVerify(dealProcessPo, "processCode");
        boolean isOk = dealProcessService.delete(dealProcessPo);
        return R.ok(isOk);
    }

    /**
     * 网上办事-门户提交处置
     *
     * @return
     */
    @PostMapping("/doorSubmit")
    public R doorSubmit(@RequestBody DoorSubmitPo doorSubmitPo, HttpServletRequest request) {
        boolean isOk = dealProcessService.doorSubmit(doorSubmitPo, request);
        return R.ok(isOk);
    }

    /**
     * 幂等检测 是否可以进行下一步（只有需要处置的才调用接口）
     *
     * @param doorSubmitPo
     * @param request
     * @return
     */
    @PostMapping("/check")
    public R check(@RequestBody DoorSubmitPo doorSubmitPo, HttpServletRequest request) {
        boolean isOk = dealProcessService.check(doorSubmitPo, request);
        return R.ok(isOk);
    }

    /**
     * 办事业务处置-分页
     *
     * @param disposePagePo
     * @param request
     * @return
     */
    @PostMapping("/disposePageQuery")
    public R disposePageQuery(@RequestBody DisposePagePo disposePagePo, HttpServletRequest request) {
        Page<DisposePageVo> disposePageVoPage = dealProcessService.disposePageQuery(new Page(disposePagePo.getCurrent(), disposePagePo.getSize()), disposePagePo, request);
        return R.ok(disposePageVoPage);
    }

    /**
     * 办事业务处置-处置
     *
     * @param disposePagePo
     * @param request
     * @return
     */
    @PostMapping("/disposeSave")
    public R disposeSave(@RequestBody DisposePagePo disposePagePo, HttpServletRequest request) {
        IBeanUtil.paramVerify(disposePagePo, "nodeState", "dataDesc");
        boolean isOk = dealProcessService.disposeSave(disposePagePo, request);
        return R.ok(isOk);
    }

    /**
     * 办事业务处置-查看处置详情
     *
     * @param disposeDetailPo
     * @return
     */
    @PostMapping("/disposeDetail")
    public R disposeDetail(@RequestBody DisposeDetailPo disposeDetailPo) {
        IBeanUtil.paramVerify(disposeDetailPo, "code");
        DisposeDetailVo disposeDetailVo = dealProcessService.disposeDetail(disposeDetailPo);
        return R.ok(disposeDetailVo);
    }

    /**
     * 撤销
     *
     * @param revocationPo
     * @return
     */
    @PostMapping("/revocation")
    public R revocation(@RequestBody RevocationPo revocationPo) {
        boolean isOk = dealProcessService.revocation(revocationPo);
        return R.ok(isOk);
    }

    /**
     * 流程记录
     *
     * @param dealProcessPo
     * @return
     */
    @PostMapping("/record")
    public R record(@RequestBody DealProcessPo dealProcessPo) {
        IBeanUtil.paramVerify(dealProcessPo, "recordCode");
        List<DealRecordEntity> dealRecordEntities = dealProcessService.record(dealProcessPo);
        return R.ok(dealRecordEntities);
    }


    /**
     * 门户-按类别
     *
     * @return
     */
    @PostMapping("/categoryDoor")
    public R categoryDoor() {
        List<CategoryDoorVo> categoryDoorVos = dealProcessService.categoryDoor();
        return R.ok(categoryDoorVos);
    }

    /**
     * 数据统计
     *
     * @return
     */
    @PostMapping("/dataCount")
    public R dataCount() {
        DataCountVo dataCountVo = dealProcessService.dataCount();
        return R.ok(dataCountVo);
    }

    /**
     * 事项办结率排行榜
     *
     * @return
     */
    @PostMapping("/ranking")
    public R ranking() {
        List<RankingVo> rankingVos = dealProcessService.ranking();
        return R.ok(rankingVos);
    }

    /**
     * 门户个人中心-分页
     *
     * @param centerPageQueryPo
     * @return
     */
    @PostMapping("/centerPageQuery")
    public R centerPageQuery(@RequestBody CenterPageQueryPo centerPageQueryPo) {
        Page<CenterPageQueryVo> centerPageQueryVoPage = dealProcessService.centerPageQuery(new Page(centerPageQueryPo.getCurrent(), centerPageQueryPo.getSize()), centerPageQueryPo);
        return R.ok(centerPageQueryVoPage);
    }

    /**
     * 导出办事记录
     */
    @PostMapping("/dealProcessDownload")
    public R<Boolean> dealProcessDownload(@RequestBody DisposePagePo disposePagePo, HttpServletRequest request,HttpServletResponse response){
        disposePagePo.setSize(9999);
        List<DisposePageVo> disposePageVoPage = dealProcessService.disposePageQuery(new Page(disposePagePo.getCurrent(), disposePagePo.getSize()), disposePagePo, request).getRecords();
        return R.ok(dealProcessService.dealProcessDownload(disposePageVoPage,response));
    }
}
